import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import jieba
import wordcloud
from imageio import imread
plt.rcParams['font.sans-serif'] = ['SimHei'] # 在matplotlib中设置字体为SimHei黑体
plt.rcParams['axes.unicode_minus'] = False# 解决matplotlib坐标轴负号‘-’显示为方块问题
df1 = pd.read_excel(r"C:\Users\gcc\Desktop\nanjingfood.xlsx")
df2=pd.read_excel(r"C:\Users\gcc\Desktop\suzhou.xlsx")
df3=pd.read_excel(r"C:\Users\gcc\Desktop\wuxifood.xlsx")
def read(df1):
    lis=[]
    t5= np.array(df1["字段1"])[:1]
    t5=t5[0].split("票")[0]
    lis.append(eval(t5))
    t4 = np.array(df1["字段2"])[:1]
    t4 = t4[0].split("票")[0]
    lis.append(eval(t4))
    t3 = np.array(df1["字段3"])[:1]
    t3 = t3[0].split("票")[0]
    lis.append(eval(t3))
    t2 = np.array(df1["字段4"])[:1]
    t2 = t2[0].split("票")[0]
    lis.append(eval(t2))
    t1 = np.array(df1["字段5"])[:1]
    t1 = t1[0].split("票")[0]
    lis.append(eval(t1))
    return(lis)
def changetocsv():#制作南京top1夫子庙美食街词云图
    #df=df1['rev-txt']
    df=df2['rev-txt']
    df=pd.DataFrame(df)
    df.to_csv(r"C:/Users/gcc/Desktop/suzhouTop1.csv",index=False,encoding="utf-8")
def wordclo():
    jieba.add_word("生煎",freq=None,tag=None)
    with open("C:/Users/gcc/Desktop/suzhouTop1.csv", "r", encoding="utf-8") as f:
        txt = f.read()
    ex = ["，", "（", "）", "\n", "的", "《", "》", " ", "\"", "了", "在", "是", "你", "我", "她", "他", "。", "？", "、", ".", "和",
          "等", "对", "都", "各", "“", "“", "”", "要", "就", "很", "还","有","也","吧","！","啊"]
    for i in ex:
        txt = txt.replace(i, "")
    words = jieba.lcut(txt)
    d = {}
    for i in words:
        d[i] = d.get(i, 0) + 1
    items = d.items()
    ite = list(items)
    ite.sort(key=lambda x: x[-1], reverse=True)
    ite = ite[:100]
    dic = dict(ite)
    print(dic)
    lis = list(dic.keys())
    txt = " ".join(lis)
    jpg = imread(r'C:/Users/gcc/Desktop/苏州.jpg')
    w = wordcloud.WordCloud(font_path="msyh.ttc", width=1000, height=800, max_words=100, background_color="white",mask=jpg)
    w.generate(txt)
    w.to_file("b.png")
def main():
    lis1=list(read(df1))
    lis2=list(read(df2))
    lis3=list(read(df3))
    print(lis1)
    print(lis2)
    print(lis3)
   # plt.figure(figsize=(20, 10))
    x = np.arange(5)
    tick_label = ['五星', '四星', '三星','二星','一星']
    bar_width = 0.3
    plt.bar(x, lis1, bar_width, color='salmon', align='center', label='南京')
    plt.bar(x + bar_width, lis2, bar_width, color='orchid', align='center', label='苏州')
    plt.bar(x + 2 * bar_width, lis3, bar_width, color='orange', align='center', label='无锡')
    plt.legend()
    plt.xticks(x + bar_width, tick_label)
    plt.xlabel("顾客评分")
    plt.ylabel("评分个数")
    plt.title("三大城市top1餐饮好评对比图")
    plt.tight_layout()
    plt.show()
wordclo()
